﻿using DevExpress.XtraGrid.Views.BandedGrid;
using DLL;
using NursingFormSystem.Model;
using NursingFormSystem.Transfusion;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.PICCBloodPurification
{
    public partial class HistoryPICCBloodPurification : Form
    {
        public NursingFormSystem.NurFormsService.NursingForm formService = new NursingFormSystem.NurFormsService.NursingForm();
        PatientInfo patientinfo;
        Tb_User LoginUser;
        public HistoryPICCBloodPurification(Tb_User loginUser, PatientInfo patientInfo)
        {
            InitializeComponent();
            this.patientinfo = patientInfo;
            this.LoginUser = loginUser;
            BindControl();
            BindInfo(null);
        }

        #region GridView设置
        private void BindControl()
        {
            this.StatDate.DateTime = DateTime.Now.AddDays(-30);
            this.EndDate.DateTime = DateTime.Now;
            foreach (GridBand gridBand in this.bandedGridView1.Bands)//设置居中
            {
                foreach (GridBand gridBandSecond in gridBand.Children)
                {
                    gridBandSecond.OptionsBand.FixedWidth = true;
                    gridBandSecond.AppearanceHeader.Options.UseTextOptions = true;
                    gridBandSecond.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
                    gridBandSecond.AppearanceHeader.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center;
                }
                gridBand.OptionsBand.FixedWidth = true;
                gridBand.AppearanceHeader.Options.UseTextOptions = true;
                gridBand.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
                gridBand.AppearanceHeader.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center;
            }
        }
        #endregion

        #region 查询方法，共用
        /// <summary>
        /// 根据日期区间进行查询
        /// </summary>
        /// <param name="num">次数 为空则查全部 否则查相对应次数的数据</param>
        public void BindInfo(string num)
        {
            if (this.patientinfo == null) return;
            int formId = (int)FormType.BloodPurification;  //表单ID
            string patientid = this.patientinfo.PATIENT_ID;//住院号
            int VisitId = Convert.ToInt32(patientinfo.VISIT_ID);//住院次数
            DataTable dt = new DataTable();
            dt.Columns.Add("riqi", typeof(string));//日期
            dt.Columns.Add("shijian", typeof(string));//时间
            dt.Columns.Add("zhiliaomoshi", typeof(string));//治疗模式
            dt.Columns.Add("xueyatonglu", typeof(string));//血管通路
            dt.Columns.Add("lvqixinghao", typeof(string));//过滤型号
            dt.Columns.Add("kangningfangshi", typeof(string));//抗凝方式
            dt.Columns.Add("shengmingtizhengT", typeof(string));//生命体征--T
            dt.Columns.Add("shengmingtizhengHR", typeof(string));//生命体征--HR
            dt.Columns.Add("shengmingtizhengR", typeof(string));//生命体征--R
            dt.Columns.Add("shengmingtizhengBP", typeof(string));//生命体征--BP
            dt.Columns.Add("yalijianceshuruya", typeof(string));//压力检测--输入压
            dt.Columns.Add("yalijiancehuishuya", typeof(string));//压力检测--回输压
            dt.Columns.Add("yalijianceTMP", typeof(string));//压力检测--TMP
            dt.Columns.Add("chufangxueliuliang", typeof(string));//处方--血流量
            dt.Columns.Add("chufangPBP", typeof(string));//处方--PBP
            dt.Columns.Add("chufangtouxiye", typeof(string));//处方--透析液
            dt.Columns.Add("chufangzhihuanyeqian", typeof(string));//处方--置换液--前
            dt.Columns.Add("chufangzhihuanyehou", typeof(string));//处方--置换液--后
            dt.Columns.Add("chufangtuoshuiliang", typeof(string));//处方--脱水量
            dt.Columns.Add("chufangkangningji", typeof(string));//处方--抗凝剂
            dt.Columns.Add("bingqingguanchajichuli", typeof(string));//病情观察及措施
            dt.Columns.Add("hushi", typeof(string));//护士
            dt.Columns.Add("Id", typeof(string));//ID隐藏，双击触发事件
            var ret = formService.GetNursingFormRecordByDate(patientid, VisitId, formId, StatDate.DateTime, EndDate.DateTime);
            if (ret.Code == 1)
            {
                List<NursingFormRecord> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NursingFormRecord>>(ret.D);
                list = list.OrderByDescending(p => p.AssessmentDate).ToList();
                foreach (NursingFormRecord record in list)
                {
                    var NursingFormData = record.NursingFormData;
                    BloodPurification ldf = new BloodPurification();
                    ldf = XmlUtil.XMLToObject<BloodPurification>(NursingFormData, Encoding.UTF8);
                    DataRow row = dt.NewRow();
                    string RiQi = ldf.RiQi;
                    DateTime date = Convert.ToDateTime(RiQi);
                    row["riqi"] = date.ToString("yyyy-MM-dd");   //日期
                    row["shijian"] = DateTimeHelper.HHmmssToHHmm(ldf.ShiJian);  //时间
                    row["zhiliaomoshi"] = ldf.ZhiLiaoMoShi;    //治疗模式
                    row["xueyatonglu"] = ldf.XueGuanTongLu;   //血管通路
                    row["lvqixinghao"] = ldf.GuoLvHingHao;   //过滤型号
                    row["kangningfangshi"] = ldf.KangNingFangShi;  //抗凝方式
                    row["shengmingtizhengT"] = ldf.ShengMingTiZhengT;   //生命体征--T
                    row["shengmingtizhengHR"] = ldf.ShengMingTiZhengHR;  //生命体征--HR
                    row["shengmingtizhengR"] = ldf.ShengMingTiZhengR;    //生命体征--R
                    row["shengmingtizhengBP"] = ldf.ShengMingTiZhengBP;  //生命体征--BP
                    row["yalijianceshuruya"] = ldf.YaLiJianCeShuRuYa;   //压力检测--输入压
                    row["yalijiancehuishuya"] = ldf.YaLiJianCeHuiShuYa;  //压力检测--回输压
                    row["yalijianceTMP"] = ldf.YaLiJianCeTMP;            //压力检测--TMP
                    row["chufangxueliuliang"] = ldf.ChuFangXueLiuLiang;       //处方--血流量
                    row["chufangPBP"] = ldf.ChuFangPBP;                         //处方--PBP
                    row["chufangtouxiye"] = ldf.ChuFangTouXiYe;              //处方--透析液
                    row["chufangzhihuanyeqian"] = ldf.ChuFangZhiHuanYeQian;      //处方--置换液--前
                    row["chufangzhihuanyehou"] = ldf.ChuFangZhiHuanYeHou;        //处方--置换液--后
                    row["chufangtuoshuiliang"] = ldf.ChuFangTuoShuiLiang;        //处方--脱水量
                    row["chufangkangningji"] = ldf.ChuFangKangNingJi;           //处方--抗凝剂
                    row["bingqingguanchajichuli"] = ldf.BingQingGuanChaJiChuLi;      //病情观察及措施
                    row["hushi"] = ldf.HuShi;        //护士
                    row["Id"] = record.Id;    //ID隐藏，双击触发事件
                    if (string.IsNullOrEmpty(num))
                        dt.Rows.Add(row);
                    if (!string.IsNullOrEmpty(num) && Convert.ToInt32(num) == ldf.Num)
                        dt.Rows.Add(row);
                }

            }
            dt.DefaultView.Sort = "RiQi DESC,ShiJian DESC";
            DataTable data = dt.DefaultView.ToTable();
            gridControl1.DataSource = data;
        }

        #endregion

        #region 查询
        private void btnSearch_Click(object sender, EventArgs e)
        {
            BindInfo(null);
        }
        #endregion

        #region 新增
        private void btnAdd_Click(object sender, EventArgs e)
        {
            int formId = (int)FormType.BloodPurification;  //表单ID
            string patientid = this.patientinfo.PATIENT_ID;//住院号
            int VisitId = Convert.ToInt32(patientinfo.VISIT_ID);//住院次数
            string sql = @" SELECT TOP 1 NursingFormData FROM NursingFormRecord(NOLOCK) WHERE formId = {0} AND patientid = '{1}' AND VisitId = {2}  ORDER BY Id desc ";
            sql = string.Format(sql, formId, patientid, VisitId);
            object obj = SqlHelper.ExecuteScalar(sql);
            //查询数据 有数据的时候
            if (obj != null && obj != DBNull.Value && !string.IsNullOrEmpty(obj.ToString()))
            {
                var NursingFormData = obj.ToString();
                BloodPurification ldf = new BloodPurification();
                ldf = XmlUtil.XMLToObject<BloodPurification>(NursingFormData, Encoding.UTF8);
                //判断 新起一次之后再点击新增按钮
                if (ldf.Num > 1)
                {
                    AddPICCBloodPurification acc = new AddPICCBloodPurification(LoginUser, patientinfo, "", "N", ldf.Num.ToString());
                    acc.ShowDialog();
                    if (acc.DialogResult == DialogResult.OK)
                    {
                        BindInfo(null);
                    }
                }
                else
                {
                    AddPICCBloodPurification acc = new AddPICCBloodPurification(LoginUser, patientinfo, "", "N");
                    acc.ShowDialog();
                    if (acc.DialogResult == DialogResult.OK)
                    {
                        BindInfo(null);
                    }
                }
            }
            else
            {
                AddPICCBloodPurification acc = new AddPICCBloodPurification(LoginUser, patientinfo, "", "N");
                acc.ShowDialog();
                if (acc.DialogResult == DialogResult.OK)
                {
                    BindInfo(null);
                }
            }
        }
        #endregion

        #region 修改
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (bandedGridView1.RowCount > 0)
            {
                string Id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
                AddPICCBloodPurification acc = new AddPICCBloodPurification(LoginUser, patientinfo, Id, "N");
                acc.ShowDialog();
                if (acc.DialogResult == DialogResult.OK)
                {
                    BindInfo(null);
                }
            }
        }
        #endregion

        #region 打印
        private void btnPrint_Click(object sender, EventArgs e)
        {
            PrintPICCBoold pp = new PrintPICCBoold(patientinfo, StatDate.DateTime, EndDate.DateTime);
            pp.Show();

        }
        #endregion

        #region 删除
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认要删除数据吗？", "删除前确认", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                string id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
                var ret2 = formService.DeleteNursingFormRecord(id);
                MessageBox.Show(ret2.M);
                if (ret2.Code == 1)
                {
                    BindInfo(null);
                }
            }
        }
        #endregion

        #region 双击事件，显示对应的值
        private void bandedGridView1_DoubleClick(object sender, EventArgs e)
        {
            string Id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
            AddPICCBloodPurification acc = new AddPICCBloodPurification(LoginUser, patientinfo, Id, "N");
            acc.ShowDialog();
            if (acc.DialogResult == DialogResult.OK)
            {
                BindInfo(null);
            }
        }
        #endregion

        #region  历史记录
        private void btnHis_Click(object sender, EventArgs e)
        {
            HistoryBP f = new HistoryBP(patientinfo);
            f.ShowDialog();
            if (f.DialogResult != DialogResult.OK) return;
            if (string.IsNullOrEmpty(f.Id)) return;
            BindInfo(f.Num);
        }
        #endregion

        #region 新起一次
        private void btnNewNum_Click(object sender, EventArgs e)
        {
            int formId = (int)FormType.BloodPurification;  //表单ID
            string patientid = this.patientinfo.PATIENT_ID;//住院号
            int VisitId = Convert.ToInt32(patientinfo.VISIT_ID);//住院次数
            string sql = @" SELECT TOP 1 NursingFormData FROM NursingFormRecord(NOLOCK) WHERE formId = {0} AND patientid = '{1}' AND VisitId = {2}  ORDER BY Id desc ";
            sql = string.Format(sql, formId, patientid, VisitId);
            object obj = SqlHelper.ExecuteScalar(sql);
            if (obj != null && obj != DBNull.Value && !string.IsNullOrEmpty(obj.ToString()))
            {
                var NursingFormData = obj.ToString();
                BloodPurification ldf = new BloodPurification();
                ldf = XmlUtil.XMLToObject<BloodPurification>(NursingFormData, Encoding.UTF8);
                AddPICCBloodPurification acc = new AddPICCBloodPurification(LoginUser, patientinfo, "", "Y", ldf.Num.ToString());
                acc.ShowDialog();
                if (acc.DialogResult == DialogResult.OK)
                {
                    BindInfo(null);
                }
            }
            else
            {
                AddPICCBloodPurification acc = new AddPICCBloodPurification(LoginUser, patientinfo, "", "Y");
                acc.ShowDialog();
                if (acc.DialogResult == DialogResult.OK)
                {
                    BindInfo(null);
                }
            }
        }
        #endregion
    }
}